package vg.modules.search.components.searchTreePanel.components;



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Set;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

import vg.modules.search.SearchPluginGlobals;
import vg.modules.search.components.FBSecondPanel;
import vg.services.data_base_manager.data.graph.Graph;
import vg.services.data_base_manager.data.skeleton.GraphTreeElement;
import vg.services.graph_view_manager.interfaces.graph_view.AGraphView;
import vg.services.graph_view_manager.interfaces.graph_view.IGraphRenderer;
import vg.services.graph_view_manager.realization.graph_view.smart_graph_view.realization.actions.ShowAttributesAction;
import vg.services.main_manager.MainManager;
import vg.services.plugin_manager.event_and_request.request.UIRequestAddTab;
import vg.services.plugin_manager.event_and_request.request.UIRequestReplaceCurrentTab;
import vg.services.plugin_manager.realization.PluginParameter;

/**
 * This class realizes menu item for opening
 * of subgraph in new tab.
 *
 * @author tzolotuhin
 */
public class OpenSubGraphMenuItem implements IMenuItem {
	private final JMenuItem jItem;
	private final OpenSubGraphAction action;

	public OpenSubGraphMenuItem(final JTree tree, final FBSecondPanel fbSecondPanel, final PluginParameter parameter) {
		this.action = new OpenSubGraphAction(tree, fbSecondPanel, parameter);
		this.jItem = new JMenuItem("Open subgraph");
		final Icon icon = new ImageIcon("data/resources/textures/workflow.png");
		this.jItem.setIcon(icon);
		this.jItem.addActionListener(this.action);
	}

    @Override
	public JMenuItem getMenuItem() {
		return(this.jItem);
	}

    @Override
    public boolean isVisible() {
		return(this.action.isVisible());
	}

    @Override
    public void updateTheme() {
		SwingUtilities.updateComponentTreeUI(this.jItem);
	}
}

/**
 * Opens subgraph in new tab.
 *
 * @author tzolotuhin
 */
class OpenSubGraphAction implements ActionListener {
	private final JTree tree;
	private final FBSecondPanel fbSecondPanel;
	private final PluginParameter parameter;

	public OpenSubGraphAction(final JTree tree, final FBSecondPanel fbSecondPanel, final PluginParameter parameter) {
		this.tree = tree;
		this.fbSecondPanel = fbSecondPanel;
		this.parameter = parameter;
	}
	/**
	 * Action
	 */
	public void actionPerformed(ActionEvent e) {
		MainManager.logger.printDebug("[" + this.getClass().getName()+".actionPerformed] [PROCESS] Openning of subgraph in new tab(from navigator).");
		TreePath[] p = tree.getSelectionPaths();
		if(p != null && p.length == 1) {
			MainManager.logger.printDebug("[" + this.getClass().getName()+".actionPerformed] [OK] Openning of subgraph in new tab(from navigator).");
			TreeModel tm = this.tree.getModel();
			if(tm == null) {
				MainManager.logger.printDebug("[" + this.getClass().getName()+".actionPerformed] [BAD] Openning of subgraph in new tab(from navigator). Model = null.");
			} else {
				GraphTreeElement node = ((GraphTreeElement)tm.getRoot()).vg_getNode(p[0]);
				if(node.vg_getGraphId_tmp() >= 0) {
					Graph ssg = this.parameter.graphDataBase.getGraph(node.vg_getGraphId_tmp());
					Set<String> vertexAdditionalAttributes = this.fbSecondPanel.getVertexAdditionalAttributes();

					final AGraphView igv = MainManager.graphViewManager.newGraphView(ssg);
					igv.addAction(new ShowAttributesAction(vertexAdditionalAttributes, vertexAdditionalAttributes) {
                        @Override
                        public void finishAction(IGraphRenderer graphRenderer) {
                            if (fbSecondPanel.getActionMode() == SearchPluginGlobals.DEF_ACTION_MODE_1) {
                                UIRequestAddTab req = new UIRequestAddTab(igv);
                                parameter.userInterfaceManager.sendRequest(req);
                            } else {
                                UIRequestReplaceCurrentTab req = new UIRequestReplaceCurrentTab(igv, null);
                                parameter.userInterfaceManager.sendRequest(req);
                            }
                        }
                    });
				} else {
					MainManager.windowMessage.warningMessage("Selection should be subgraph, if you want to open subgraph in new tab", "Opening of subgraph");				
				}
			}
		} else {
			MainManager.logger.printDebug("[" + this.getClass().getName()+".actionPerformed] [BAD] Openning of subgraph in new tab(from navigator).");
			MainManager.windowMessage.warningMessage("Selection should have one node, if you want to open subgraph in new tab", "Opening of subgraph");
		}
	}
	public boolean isVisible() {
		if(this.tree != null) {
			TreePath[] p = this.tree.getSelectionPaths();
			if(p != null && p.length == 1) {
				TreeModel tm = this.tree.getModel();
				if(tm != null) {
					GraphTreeElement node = ((GraphTreeElement)tm.getRoot()).vg_getNode(p[0]);
					if(node.vg_getGraphId_tmp() >= 0) {
						return true;
					}
				}
			}
		}
		return false;
	}
}
